package enterprise.manage.web.controller;

import enterprise.manage.dao.UserDao;
import enterprise.manage.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController; 
import enterprise.manage.entity.Partner;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by IntelliJ IDEA.
 * User: ChiBing
 * Date: 2010-8-30
 * Time: 14:32:45
 * Dsc:
 */
public class ChangePartnerPasswordController extends SimpleFormController {
    /**
     * 日志记录
     */

    private static final Logger LOGGER = LoggerFactory.getLogger(ChangePartnerPasswordController.class);
    /**
     * 商户主视图
     */
    private static final String MAIN_VIEW = "partner/partner_main";
    /**
     * 修改密码视图
     */
    private static final String CHANGEPASSWORD_VIEW = "partner/partner_changepassword";
    /**
     * /**
     * 商户DAO
     */
    @Autowired
    private UserDao userDao;


    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response,
                                    Object command, BindException errors)
            throws Exception {
        ChangePartnerPasswordCommand changePartnerPasswordCommand = (ChangePartnerPasswordCommand) command;
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user.getPsw().equals(changePartnerPasswordCommand.getOldPartnerPassword())) {
            user.setPsw(changePartnerPasswordCommand.getNewPartnerPassword());
            userDao.save(user);
            request.setAttribute("msg", "修改密码成功！");
            return new ModelAndView(MAIN_VIEW);
        } else {
            request.setAttribute("error", "您输入的密码有误！");
            request.setAttribute("changePartnerPasswordCommand", changePartnerPasswordCommand);
            return new ModelAndView(CHANGEPASSWORD_VIEW);
        }

    }

}
